package com.mikepenz.interactor.imp;

import android.content.Context;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.android.volley.Request;
import com.android.volley.VolleyError;
import com.mikepenz.bean.RequestBean;
import com.mikepenz.presenter.listener.RequestListener;
import com.mikepenz.utils.MethodCode;
import com.mikepenz.utils.MethodType;
import com.quickdv.helper.NetWorkImp;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by lilongfei on 15/11/8.
 */
public class RegistInteractorImp extends NetWorkImp {
    RequestListener listener;
    public RegistInteractorImp(Context c,RequestListener listener) {
        super(c);
        this.listener = listener;
    }

    public void getSms(String phone){

        RequestBean bean = new RequestBean();
        Map<String, Object> params = new HashMap<>();
        params.put("tel", phone);
        bean.setRequestParams(params);
        bean.setRequestCode(MethodType.GET_SMS);
        addFastJsonQueue(Request.Method.POST, MethodType.GET_SMS, (JSONObject) JSON.toJSON(bean));
        startRequest();
    }

    public void Regist(String phone ,String pwd,String sms){

        RequestBean bean = new RequestBean();
        Map<String, Object> params = new HashMap<>();
        params.put("userName", phone);
        params.put("passWord", pwd);
        params.put("sms",sms);
        bean.setRequestParams(params);
        bean.setRequestCode(MethodType.REGIST);
        addFastJsonQueue(Request.Method.POST, MethodType.REGIST, (JSONObject) JSON.toJSON(bean));
        startRequest();
    }

    @Override
    protected void onRequestSuccess(Object tag, JSONObject result) {
        if (!isOk(result)) {
            listener.Error(tag + "", "返回数据格式有误");
            return;
        }
        JSONObject response = result.getJSONObject(MethodCode.responseContent_key);
        int statue = response.getInteger(MethodCode.responseStatue);

        if (tag.equals(MethodType.REGIST)) {
            if (statue == (MethodCode.NetState.SUCCESS.getValue())) {
                listener.Success(tag + "", null);
            } else
                listener.Error(tag + "", "注册失败");
        }
    }

    @Override
    protected void onRequestExcption(VolleyError error) {
        listener.Exception(null, error);
    }
}
